window.onload = function (){
    initInfo();
    findByItem(1);
}

function initInfo(){
    axios.get("/user/getLoginUser").then(resp =>{
        if(resp.data == ""){
            $("infoDiv").innerHTML = "请先<a href='/login.html'>登陆</a>";
        }
        else{
            $("infoDiv").innerHTML = "欢迎光临，"+resp.data.name+
                "<input type='button' style='margin-left: 200px;' value='安全退出' onclick='exitSystem()'>";
        }
    })
}

function exitSystem(){
    axios.get("/user/destorySession").then(resp =>{
       if(resp.data == "ok"){
           initInfo();
       }
    });
}

function findByItem(pageNO){
    axios.get("/product/findByItem",{
        params: {
            pageNO,
            name:$("name").value
        }
    }).then(resp =>{
        var info = resp.data;
        var str = "";
        info.records.forEach(n =>{
           str += `<div style="width: 400px;height: 300px;">
                <img src="/productImg/${n.imgPath}" width="200" height="200"><br>
                商品名：${n.name}<br>
                单价：${n.price}<br>
                评论：${n.speakNum}<br>
                <a href="/speak.html?id=${n.id}">查看</a>
            </div>`;
        });
        $("productDiv").innerHTML = str;

        var cutStr = "";
        for(var i=1;i<=info.pages;i++){
            cutStr += `<a style="margin-left: 10px" href="javascript:findByItem(${i})">${i}</a>`;
        }
        $("cutDiv").innerHTML = cutStr;
    })
}